//
//  QBEmoticonModel.swift
//  04-表情键盘
//
//  Created by bing on 16/4/1.
//  Copyright © 2016年 bing. All rights reserved.
//

import UIKit
/// 表情模型
class QBEmoticonModel: NSObject,NSCoding{
    
    // 模型所在的文件夹
    var id: String
    
    // emoji 的16进制字符串
    var code: String? {
        didSet {
            // 将 code转成emoji字符串
            
            let scanner = NSScanner(string: code!)
            
            var result: UInt32 = 0
            scanner.scanHexInt(&result)
            
            emoji = String(Character(UnicodeScalar(result)))
        }
    }
    
    var emoji: String?
    
    // 表情名称,用于网络传输
    var chs: String?
    
    // 表情图片名称，不能用于显示图片，需要路劲
    var png: String? {
        didSet {
            // 拼接全路径 = Emoticons.bundle/id/png
            fullPngPath = QBEmoticonPackageManager.bundlePath + "/" + id + "/" + png!
        }
    }
    
    var fullPngPath: String?
    
    /// 字典转模型
    init(id: String, dict: [String: String]) {
        
        self.id = id
        
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        return "\n \t 表情模型:code: \(code), chs: \(chs), png: \(png)"
    }
    
    /// 将表情模型里面的图片转成属性文本
    func emoticonModelToArrT(font: UIFont) ->NSAttributedString {
        
        // 图片->附件->属性文本
        let attachment = QBTextAttachment()
        attachment.image = UIImage(contentsOfFile: fullPngPath!)
        
        // 设置表情名称给附件
        attachment.chs = self.chs
        
        let wh = (font.lineHeight ?? 20) * 0.9
        // bounds和frame正好相反,往下移是负数
        attachment.bounds = CGRect(x: 0, y: -3, width: wh, height: wh)
        
        // 转换成可变的属性字体
        let attrM = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        
        // 给附件设置UIFont属性，不然后面的表情会很小
        attrM.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        return attrM
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(emoji, forKey: "emoji")
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        //aCoder.encodeObject(fullPngPath, forKey: "fullPngPath")
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        id = aDecoder.decodeObjectForKey("id") as? String ?? ""
        code = aDecoder.decodeObjectForKey("code") as? String
        emoji = aDecoder.decodeObjectForKey("emoji") as? String
        chs = aDecoder.decodeObjectForKey("chs") as? String
        png = aDecoder.decodeObjectForKey("png") as? String
        /*
        /Users/bing/Library/Developer/CoreSimulator/Devices/D2968B37-9C3E-4653-8169-41B2868ED8C1/data/Containers/Data/Application/AD2219D6-2FF1-4F58-9323-4E8FFDA39FB6
        
        /Users/bing/Library/Developer/CoreSimulator/Devices/D2968B37-9C3E-4653-8169-41B2868ED8C1/data/Containers/Data/Application/BC108CD5-3C86-448F-AD2D-F3027FE58C24
        */
        //fullPngPath = aDecoder.decodeObjectForKey("fullPngPath") as? String
        // 重新拼接全路径，因为在模拟器上重新运行沙盒的路径会变化fullPngPath会失效
        if let p = png {
            fullPngPath = QBEmoticonPackageManager.bundlePath + "/" + id + "/" + p
        }
    }
}
